package cn.edu.nju.software.bean.show;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import cn.edu.nju.software.bean.storage.Order;

public class OrderShow {

	private int orderID;
	private UserShow user;
	private BigDecimal charge;
	private int status;
	private String address;
	private String submitTime;
	private String dispatchTime;
	private String confirmTime;
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");

	public OrderShow(Order order, UserShow user) {
		this.orderID = order.getOrderID();
		this.user = user;
		this.charge = order.getCharge();
		this.status = order.getStatus();
		this.address = order.getAddress();
		if (order.getSubmitTime() != null) {
			this.submitTime = dateFormat.format(order.getSubmitTime());
		}
		if (order.getDispatchTime() != null) {
			this.dispatchTime = dateFormat.format(order.getDispatchTime());
		}
		if (order.getConfirmTime() != null) {
			this.confirmTime = dateFormat.format(order.getConfirmTime());
		}
	}

	public int getOrderID() {
		return orderID;
	}

	public void setOrderID(int orderID) {
		this.orderID = orderID;
	}

	public UserShow getUser() {
		return user;
	}

	public void setUser(UserShow user) {
		this.user = user;
	}

	public BigDecimal getCharge() {
		return charge;
	}

	public void setCharge(BigDecimal charge) {
		this.charge = charge;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getSubmitTime() {
		return submitTime;
	}

	public void setSubmitTime(String submitTime) {
		this.submitTime = submitTime;
	}

	public void setSubmitTime(Timestamp submitTime) {
		if (submitTime != null) {
			this.submitTime = dateFormat.format(submitTime);
		}
	}

	public String getDispatchTime() {
		return dispatchTime;
	}

	public void setDispatchTime(String dispatchTime) {
		this.dispatchTime = dispatchTime;
	}

	public void setDispatchTime(Timestamp dispatchTime) {
		if (dispatchTime != null) {
			this.dispatchTime = dateFormat.format(dispatchTime);
		}
	}

	public String getConfirmTime() {
		return confirmTime;
	}

	public void setConfirmTime(String confirmTime) {
		this.confirmTime = confirmTime;
	}

	public void setConfirmTime(Timestamp confirmTime) {
		if (confirmTime != null) {
			this.confirmTime = dateFormat.format(confirmTime);
		}
	}
	
	public Order getOrder() {
		try {
			Timestamp submitTime = null, dispatchTime = null, confirmTime = null;
			if (this.submitTime != null) {
				submitTime = new Timestamp(dateFormat.parse(this.submitTime).getTime());
			}
			if (this.dispatchTime != null) {
				new Timestamp(dateFormat.parse(this.dispatchTime).getTime());
			}
			if (this.confirmTime != null) {
				new Timestamp(dateFormat.parse(this.confirmTime).getTime());
			}
			return new Order(orderID, user.getUserID(), charge, status, address, submitTime, dispatchTime, confirmTime);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	
}
